//
//  Model.swift
//  FireBOOM
//
//  Created by ktiays on 2020/7/22.
//

import Foundation

class Model {
    
    // 用户ID
    public var userID: String = "USER00001"
    // 正常浓度
    public var conc: Int = 10
    // 常温
    public var temperature: Int = 28
    // 默认无火
    public var detected: Bool = false
    
    init(id: Int, conc: Int, temp: Int, detected: Bool) {
        self.userID = "USER" + String(format: "%05d", id)
        self.conc = conc
        self.temperature = temp
        self.detected = detected
    }
    
    init(_ model: Model) {
        self.userID = model.userID
        self.conc = model.conc
        self.temperature = model.temperature
        self.detected = model.detected
    }
    
    /// 发送数据至服务器
    public func send() {
        print("conc: " + self.conc.description + " temp: " + self.temperature.description + " detected: " + self.detected.description)
        Api.upload(id: userID, temperature: temperature, conc: conc, detected: (detected ? 1 : 0))
    }
    
    /// 使浓度与温度发生波动
    /// - Parameters:
    ///   - concRange: 浓度波动范围
    ///   - tempRange: 温度波动范围
    public func surge(concRange: Int, tempRange: Int) {
        self.conc = self.conc.surge(concRange)
        self.temperature = self.temperature.surge(tempRange)
    }
}
